package com.seanzx.dp.condition;

import com.seanzx.common.StringUtils;

import java.util.Arrays;
import java.util.function.BiFunction;

public enum ConditionType {
    // FIXME different type compare
    EQUALS("=", (c, v) -> StringUtils.equals(v, c.getValues().get(0))),
    BIGGER(">", (c, v) -> v.compareTo(c.getValues().get(0)) > 0),
    SMALLER("<", (c, v) -> v.compareTo(c.getValues().get(0)) < 0),
    BIGGER_EQUALS(">=", (c, v) -> v.compareTo(c.getValues().get(0)) >= 0),
    SMALLER_EQUALS("<=", (c, v) -> v.compareTo(c.getValues().get(0)) <= 0),
    LIKE("LIKE", (c, v) -> v.contains(c.getValues().get(0))),
    BETWEEN_AND("BETWEEN AND", (c, v) -> v.compareTo(c.getValues().get(0)) >= 0 && v.compareTo(c.getValues().get(1)) <= 0),
    IN("IN", (c, v) -> c.getValues().stream().anyMatch(v::equals));

    private final String signal;
    private final BiFunction<Condition, String, Boolean> function;
    ConditionType(String signal, BiFunction<Condition, String, Boolean> function) {
        this.signal = signal;
        this.function = function;
    }

    public boolean test(Condition condition, String value) {
        return function.apply(condition, value);
    }

    public static ConditionType of(String signal) {
        return Arrays.stream(values()).filter(conditionType -> conditionType.signal.equals(signal)).findFirst().orElse(null);
    }

    @Override
    public String toString() {
        return signal;
    }
}